#include "StdAfx.h"
#include "AudioOut.h"
#include "jsonreader.h"
#include "SubStructure.h"
#include "Menu.h"
#include "BackgroundImage.h"
#include "GameSceneRetry.h"

GameSceneRetry::GameSceneRetry()
{
	gray = new BackgroundImage();
	Retrymenu[0] = new Menu();
	Retrymenu[1] = new Menu();
	Retrymenu[0]->Init();
	Retrymenu[1]->Init();

	Retryimage = new Menu();
	Retryimage->Init();

	loadData = jsonreader::Instance();
	audio = AudioOut::Instance();
}

GameSceneRetry::~GameSceneRetry()
{
	delete gray;
	
	delete Retrymenu[0];
	delete Retrymenu[1];

	delete Retryimage;

	if(GrayScale != NULL)
		GrayScale ->Release();
	if(RetryMenu != NULL)
		RetryMenu ->Release();
}

GameSceneState* GameSceneRetry::Instance()
{
	if(_p_this == 0)
	{
		_p_this = new GameSceneRetry();
		_p_this->Init();
	}
	return _p_this;
}

void GameSceneRetry::Init()
{
	TickCount = 0;
	NowState = 0;
	sceneChange = 0;
	changeCount = 0;
	if(D3DXCreateTextureFromFileEx(pd3dDevice, _T("Mask.png"), 640, 480, 1, NULL,
		D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_FILTER_NONE, D3DX_FILTER_NONE, 0x00000000, NULL, NULL, &GrayScale))
		MessageBox(NULL, _T("Mask.png is not loaded!!!"), _T("ERROR"), MB_OK);
	if(D3DXCreateTextureFromFileEx(pd3dDevice, _T("GameSceneSub.png"), 512, 512, 1, NULL,
		D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_FILTER_NONE, D3DX_FILTER_NONE, 0x00000000, NULL, NULL, &RetryMenu))
		MessageBox(NULL, _T("GameSceneSub.png is not loaded!!!"), _T("ERROR"), MB_OK);
	
	RECT temp = {0, 0, 640, 480};
	D3DXVECTOR2 tempxy(0.0f, 0.0f);

	gray->Setrect(temp);
	gray->SetTexture(GrayScale);
	gray->SetInvisible(0xffffffff);
	
	for(int i = 0; i < 2; ++i)
	{
		temp.bottom = 64 + 64 * i;
		temp.top = 64 * i;
		temp.left = 0;
		temp.right = 256;
		tempxy.x = -100.0f;
		tempxy.y = 220.0f + 64.0f * i;

		Retrymenu[i]->Set_MenuState(&NowState);
		Retrymenu[i]->Set_MenuNo(i);
		Retrymenu[i]->Set_AniVec(loadData->getAni(106, RetryMenu, temp));
		Retrymenu[i]->Set_MoveVec(loadData->getMove(110));
		Retrymenu[i]->SetPos(tempxy);
	}

	temp.bottom = 196;
	temp.top = 128;
	
	tempxy.x = -100.0f;
	tempxy.y = 140.0f;
	Retryimage->Set_AniVec(loadData->getAni(106, RetryMenu, temp));
	Retryimage->Set_MoveVec(loadData->getMove(111));
	Retryimage->SetPos(tempxy);
}

void GameSceneRetry::Draw()
{
	gray->Draw();
	Retryimage->Draw();
	Retrymenu[0]->Draw();
	Retrymenu[1]->Draw();
}

void GameSceneRetry::Reset()
{
	for(int i = 0; i < 2; ++i)
	{
		Retrymenu[i]->Set_nowAniState(2);
		Retrymenu[i]->Set_nowMoveState(2);
	}
	Retryimage->Set_nowAniState(2);
	Retryimage->Set_nowMoveState(1);
	
	changeCount = 40;
	sceneChange = CHANGEIN;
	changeCount = 40;
	NowState = 0;
}

DWORD GameSceneRetry::Update(const KeyInput* input)
{
	TickCount++;
	if(TickCount% 3 || sceneChange != 0)
	{
		if(sceneChange != 0)
		{
			--changeCount;
			if(changeCount == 0 && sceneChange != CHANGEIN)
			{
				int wParam = 0;
				switch(NowState)
				{
				case 0:
					wParam = RESTARTGAME;
					break;
				case 1:
					wParam = PAUSEGAME;
					break;
				default:
					break;
				}
				Event(PRESSED, wParam);
			}
			else if(changeCount == 0)
			{
				sceneChange = 0;
			}
		}
	}
	else
	{
		if(input->VDOWN || input-> VUP)
		{
			NowState = NowState ^ 1;
			sceneChange = CHANGEIN;
			changeCount = 10;
		}
		else if(input->V_Z)
		{
			sceneChange = CHANGEOTHER;
			changeCount = 30;
			for(int i = 0; i < 2; ++i)
			{
				Retrymenu[i]->Set_nowMoveState(3);
				Retrymenu[i]->Set_nowAniState(3);
			}
			Retryimage->Set_nowMoveState(2);
			Retryimage->Set_nowAniState(3);
		}
	}
	Retryimage->Update();
	for(int i = 0; i < 2; ++i)
	{
		Retrymenu[i]->Update();
	}
	return RETRY;
}
